﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TaskManager
{
    public enum TYPE { Task, Demand, Bug, Testtask, Daily, Remind, Food, Tip };    // 定义控件类型信息

    // 此控件包含一个checkBox、label、textBox控件，是将这三个控件整合为一个可编辑标签的选择控件，并提供选中和文本变动处理事件
    public partial class checkIteam : UserControl
    {
        public checkIteam()
        {
            InitializeComponent();
            autoWidth();
            this.Invalidate();
        }

        bool isInit = false;
        public checkIteam(String Text, bool isChecked, String data, TYPE type)
        {
            InitializeComponent();
            transLabel.LabelText = Text;
            autoWidth();

            isInit = true;
            Checked = isChecked;
            this.data = data;
            IteamType = type;
            isInit = false;
        }

        public checkIteam(String Text, bool isChecked, String data, TYPE type, String noteText, Color noteColor)
        {
            InitializeComponent();
            transLabel.LabelText = Text;
            autoWidth();

            isInit = true;
            Checked = isChecked;
            this.data = data;
            IteamType = type;
            isInit = false;

            this.NoteText = noteText;
            this.NoteColor = noteColor;
        }

        protected override void OnPaintBackground(PaintEventArgs e)
        {
            //不进行背景的绘制
        }

        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams cp = base.CreateParams;
                cp.ExStyle |= 0x00000020; //WS_EX_TRANSPARENT
                return cp;
            }
        }

        protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
        {
            ////绘制panel的背景图像
            //if (BackgroundImage != null) e.Graphics.DrawImage(this.BackgroundImage, new Point(0, 0));
            //e.Graphics.DrawString(

            // 选中控件时，绘制背景色
            //if(selected)
            //{
            //    Brush brush = new SolidBrush(this.BackColor);
            //    e.Graphics.FillRectangle(brush, 0, 0, this.Width, this.Height);
            //}
        }


        private bool enable = true;
        [Description("标识当前控件是否可点击check，修改状态"), Category("自定义属性")]
        public bool Enable
        {
            get
            {
                return enable;
            }
            set
            {
                enable = value;
                transparentPanel_check.BackgroundImage = enable ? Properties.Resources.已开始 : Properties.Resources.未开始;
            }
        }

        private bool check = false;
        [Description("当前控件的Checked状态（当Enable=true时可修改）"), Category("自定义属性")]
        public bool Checked
        {
            get
            {
                return check;
            }
            set
            {
                if (enable)                       // 当控件可用
                {
                    check = value;
                    transparentPanel_check.BackgroundImage = check ? Properties.Resources.已完成 : Properties.Resources.已开始;
                }
            }
        }

        private TYPE iteam_type = TYPE.Task;  // 默认设为任务类型
        [Description("控件类型信息 TYPE"), Category("自定义属性")]
        public TYPE IteamType
        {
            get
            {
                return iteam_type;
            }
            set
            {
                iteam_type = value;

                
                // 控件类型对应的界面控件显示样式
                if (iteam_type == TYPE.Task) transparentPanel_type.BackgroundImage = Properties.Resources.任务;
                else if (iteam_type == TYPE.Demand) transparentPanel_type.BackgroundImage = Properties.Resources.需求;
                else if (iteam_type == TYPE.Bug) transparentPanel_type.BackgroundImage = Properties.Resources.BUG;
                else if (iteam_type == TYPE.Testtask) transparentPanel_type.BackgroundImage = Properties.Resources.测试;
                else if (iteam_type == TYPE.Daily) transparentPanel_type.BackgroundImage = Properties.Resources.日记2;
            }
        }


        private String data="";
        [Description("控件的附加信息"), Category("自定义属性")]
        public String Data
        {
            get
            {
                return data;
            }
            set
            {
                data = value;
            }
        }

        public delegate void checkBox_Handle(object sender);
        [Description("每当Checked属性更改时发生"), Category("自定义事件")] 
        public event checkBox_Handle CheckedChanged;
        
        // 子控件选中状态变动处理逻辑，调用自定义事件CheckedChanged
        private void checkBox_CheckedChanged(object sender, EventArgs e)
        {
            if (enable)
            {
                Checked = !check;   // 修改选中状态

                transLabel.FontColor = ShowTextColor;
                this.Invalidate();

                if (CheckedChanged != null && !isInit)
                {
                    CheckedChanged(this);
                }
            }
        }

        public delegate void button_Handle(object sender, EventArgs e, checkIteam iteam);
        [Description("当按钮点击时发生"), Category("自定义事件")]
        public event button_Handle ButtonClicked;

        // 按钮点击时，调用此逻辑
        private void button_click(object sender, EventArgs e)
        {
            if (ButtonClicked != null)
            {
                ButtonClicked(this, new EventArgs(), this);
            }
        }

        public delegate void select_Handle(object sender);
        [Description("当点击控件时发生，调用选中当前控件逻辑"), Category("自定义事件")]
        public event select_Handle This_Selected;

        public delegate void label_Handle(object sender, EventArgs e);
        [Description("每当ShowText属性更改时发生"), Category("自定义事件")] 
        public event label_Handle Text_Changed;

        // 此处为this.label的事件处理逻辑，调用自定义事件TextChanged
        private void label_TextChanged(object sender, EventArgs e)
        {
            if (Text_Changed != null) Text_Changed(sender, new EventArgs());
        }

        [Description("显示的文本信息"), Category("自定义属性")] 
        public String ShowText
        {
            get
            {
                return transLabel.LabelText;
            }
            set
            {
                transLabel.LabelText = value;
            }
        }

        private Color showTextColor = Color.White;
        [Description("显示的文本信息的显示颜色"), Category("自定义属性")]
        public Color ShowTextColor
        {
            get
            {
                return showTextColor;
            }
            set
            {
                showTextColor = value;
                transLabel.FontColor = showTextColor; 
            }
        }

        private String noteText="注释信息";
        [Description("脚注信息"), Category("自定义属性")]
        public String NoteText
        {
            get
            {
                return noteText;
            }
            set
            {
                noteText = value;
                transLabel_note.LabelText = noteText;
            }
        }

        private Color noteColor=Color.Gray;
        [Description("脚注信息的显示颜色"), Category("自定义属性")]
        public Color NoteColor
        {
            get
            {
                return noteColor;
            }
            set
            {
                noteColor = value;
                transLabel_note.FontColor = noteColor;
            }
        }


        private bool selected = false;
        [Description("是否为鼠标点击，选中状态"), Category("自定义属性")]
        public bool Selected
        {
            get
            {
                return selected;
            }
            set
            {
                selected = value;
                if (selected) setSelected(Color.FromArgb(255, 67, 168, 162));
                else setUnSelected(Color.FromArgb(255, 246, 246, 246));
            }
        }

        // 定义当前控件的选中状态
        private void setSelected(Color C)
        {
            this.BackColor = C;

            transLabel.FontColor = C;
            transLabel_note.FontColor = C; 
        }

        // 定义当前控件的未选中状态
        private void setUnSelected(Color C)
        {
            this.BackColor = C;

            transLabel.FontColor = ShowTextColor;
            transLabel_note.FontColor = noteColor;
        }

        // 重新设定控件的宽度
        public void autoWidth()
        {
            //if(autowidth) this.Width = label.Width + 30 +　80 + 50;
            //if (autowidth) this.Width = this.Width - 140;
        }

        public bool autowidth = true;
        public void setWidth(int width)
        {
            this.Width = width;
            autowidth = false;
        }


        //点击当前控件时显示为选中状态
        private void checkIteam_Click(object sender, EventArgs e)
        {
            Selected = true;                                   // 设置当前控件为选中状态
            if (This_Selected != null) This_Selected(this);    // 点击控件时，调用当前控件的选中逻辑
        }

        public delegate void doubleClick_Handle(object sender, EventArgs e);
        [Description("当双击控件时发生，调用当前控件逻辑"), Category("自定义事件")]
        public event doubleClick_Handle DoubleClick_M;

        private void checkIteam_DoubleClick(object sender, EventArgs e)
        {
            if (DoubleClick_M != null) DoubleClick_M(this, e);
        }

        // 双击标签时，进行编辑
        private void label_DoubleClick(object sender, EventArgs e)
        {
            checkIteam_Click(null, e);
            checkIteam_DoubleClick(this, e);
        }

        // 将任务类型转化为名称
        public static string[] typeNames = { "任务", "需求", "Bug", "测试", "日记", "信息" };
        public static string ToName(TYPE type)
        {
            return typeNames[(int)type];
        }

        // 鼠标移动到控件图标上时，显示类型名
        private void transparentPanel_type_MouseEnter(object sender, EventArgs e)
        {
            toolTip.Show(ToName(iteam_type), transparentPanel_type);
        }

        // 鼠标移开时，隐藏提示信息
        private void transparentPanel_type_MouseLeave(object sender, EventArgs e)
        {
            toolTip.Hide(transparentPanel_type);
        }

        private void checkIteam_SizeChanged(object sender, EventArgs e)
        {

        }

        // 更新当前控件的显示
        private void updateShow()
        {
            // 设置选中的颜色值

            // 重绘当前控件
            //this.Invalidate();

            // 重绘子控件
            transparentPanel_type.Invalidate();
            transparentPanel_check.Invalidate();
            transLabel.Invalidate();
            transLabel_note.Invalidate();
        }
    }
}
